package src.Sixteen;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class InternetContentFrame extends JFrame {

    public Collection<String> getURlConnecnnon(String urlString){
        URL url = null;
        URLConnection conn = null;
        Collection<String> urlCollection = new ArrayList<>();

        try {
            url = new URL(urlString);
            conn = url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            InputStreamReader in = new InputStreamReader(is,"UTF-8");
            BufferedReader br = new BufferedReader(in);
            String nextLine = br.readLine();
            while (nextLine != null){
                urlCollection.add(nextLine);
                nextLine = br.readLine();
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return urlCollection;
    }

    public InternetContentFrame(){
        JButton button = new JButton();
        JTextField tf_address = null;
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                String address = tf_address.getText().trim();
//                Collection urlConnection = getURLCollection(address);
//                Iterator it = urlConnection.iterator();
//                while (it.hasNext()){
//                    ta_content.append((String)it.next()+"\n");
//                }
            }
        });
    }
}
